<template>
  <a-modal
    :title="title"
    :visible="visible"
    :width="800"
    :mask-closable="false"
    :confirmLoading="loading"
    @cancel="hide"
    @ok="ok"
  >
    <a-form-model
      ref="form"
      :model="form"
      :rules="rules">
      <a-row :gutter="formLayout.gutter">
        <a-col :span="formLayout.span12">
          <a-form-model-item label="车牌号码" prop="number">
            <a-input :disabled="disabled" v-model="form.number" placeholder="请输入" />
          </a-form-model-item>
        </a-col>
        <a-col :span="formLayout.span12">
          <a-form-model-item label="选择车主">
            <!-- TODO: 选择人，关联出车位 -->
            <a-select v-model="form.userId" placeholder="请选择">
              <a-select-option
                v-for="user in userList"
                :key="user.uuid"
                :value="user.uuid">
                <div class="flex-h-hs-vc">
                  <div>
                    {{ user.realname }}
                    <span v-if="user.phone">（{{ user.phone }}）</span>
                    &nbsp;
                  </div>
                </div>
              </a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="formLayout.gutter">
        <a-col :span="formLayout.span12">
          <a-form-model-item label="支付状态" prop="status">
            <a-select :disabled="disabled" v-model="form.status" placeholder="请选择" :options="statusOptions" />
          </a-form-model-item>
        </a-col>
        <a-col :span="formLayout.span12">
          <a-form-model-item label="车位编号">
            <!-- TODO: 选择车位，关联出人 -->
            <a-select v-model="form.placeId" placeholder="请选择" :options="placeOptions" />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row v-if="form.status == 1" :gutter="formLayout.gutter">
        <a-col :span="formLayout.span12">
          <a-form-model-item label="停车费用" prop="pay">
            <a-input :disabled="disabled" v-model="form.pay" placeholder="请输入" />
          </a-form-model-item>
        </a-col>
        <a-col :span="formLayout.span12">
          <a-form-model-item label="支付方式" prop="payType">
            <a-select v-model="form.payType" placeholder="请选择" :options="payTypeOptions" />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="formLayout.gutter">
        <a-col v-if="form.status == 0" :span="formLayout.span12">
          <a-form-model-item label="停车开始时间" prop="startTime">
            <a-date-picker
              :disabled="disabled"
              v-model="form.startTime"
              show-time
              format="YYYY-MM-DD HH:mm:ss"
              valueFormat="YYYY-MM-DD HH:mm:ss"
              style="width: 100%"
            />
          </a-form-model-item>
        </a-col>
        <a-col v-if="form.status == 1" :span="formLayout.span12">
          <a-form-model-item label="停车截止时间" prop="endTime">
            <a-date-picker
              :disabled="disabled"
              v-model="form.endTime"
              show-time
              format="YYYY-MM-DD HH:mm:ss"
              valueFormat="YYYY-MM-DD HH:mm:ss"
              style="width: 100%"
            />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="formLayout.gutter">
        <a-col :span="formLayout.span24">
          <a-form-model-item label="备注">
            <a-input v-model="form.note" type="textarea" :rows="4" placeholder="请输入" />
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </a-modal>
</template>

<script>
import { getEasyUserListAPI } from '@/api/user.js'
import ImageUploader from '@/components/ImageUploader/ImageUploader.vue'
import { getDate } from '@/utils/util.js'

let originForm = null

export default {
  components: { ImageUploader },
  props: {
    payTypeOptions: {
      type: Array,
      default: () => []
    },
    placeOptions: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      statusOptions: [
        { label: '未支付', value: 0 },
        { label: '已支付', value: 1 }
      ],
      visible: false,
      loading: false,
      title: '',
      form: {
        number: '',
        userId: undefined,
        status: 0,
        pay: '',
        payType: undefined,
        placeId: undefined,
        startTime: getDate(),
        endTime: getDate(),
        note: ''
      },
      rules: {
        number: { required: true, message: '请输入车牌号码', trigger: 'blur' },
        status: { required: true, message: '请选择支付状态', trigger: 'change' },
        pay: { required: true, message: '请输入停车费用', trigger: 'blur' },
        payType: { required: true, message: '请选择支付方式', trigger: 'change' },
        startTime: { required: true, message: '请选择停车开始时间', trigger: 'change' },
        endTime: { required: true, message: '请选择停车截止时间', trigger: 'change' }
      },
      cb: null,
      userList: [],
      disabled: false
    }
  },
  created () {
    originForm = this.deepClone(this.form)
    getEasyUserListAPI().then(res => {
      this.userList = res.data.rows
    })
  },
  methods: {
    open (title, form, cb) {
      this.title = title
      this.cb = cb
      if (form) {
        this.disabled = true
        for (const key in this.form) {
          this.form[key] = form[key]
        }
      }
      this.visible = true
    },
    hide (close = true) {
      if (close) {
        this.form = this.deepClone(originForm)
        this.loading = false
        this.visible = false
      } else {
        this.loading = false
      }
    },
    ok () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          this.cb && this.cb(this.form, this.hide)
        }
      })
    }
  }
}
</script>
